package com.manlicloudboot.kubernetes;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.manlicloudboot.kubernetes.utils.CodeUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

@SpringBootApplication
@RestController
public class KubernetesApplication {

    public static void main(String[] args) {
        SpringApplication.run(KubernetesApplication.class, args);
    }

    /**
     * commandLineRunner方法被注解成了@Bean，
     * 程序在启动的时候，这个方法会被自动执行，将整个Application Context里面所有的bean都打印出来，
     * 这个方法是便于各位了解SpringBoot底层原理的，在生产环境中可以删除。
     *
     * @param ctx
     * @return
     */
    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }

    @RequestMapping("/getIp")
    public String getIp() throws UnknownHostException {
        InetAddress ip4 = Inet4Address.getLocalHost();
        return "(last update 1.1.1) IP add: "+ip4.getHostAddress();
    }

    @RequestMapping("/getIp2")
    public String getIp2() throws UnknownHostException {
        InetAddress ip4 = Inet4Address.getLocalHost();
        return "(2.2) IP add: "+ip4.getHostAddress();
    }

    @RequestMapping("/getImgCode")
    public void getImgCode(HttpServletRequest request , HttpServletResponse response) throws IOException {
        String code = CodeUtils.getCode(request , response , 4);
        System.out.println("验证码："+ code);
    }

    @RequestMapping("/getHutuImgCode")
    public void getHutuImgCode(HttpServletRequest request , HttpServletResponse response) throws IOException {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 40, 4, 4);
        lineCaptcha.setGenerator(new CodeGenerator() {
            @Override
            public String generate() {
                return RandomUtil.randomNumbers(4);
            }

            @Override
            public boolean verify(String code, String userInputCode) {
                return StrUtil.isNotBlank(userInputCode) ? StrUtil.equalsIgnoreCase(code, userInputCode) : false;
            }
        });
        //2.获得生成的验证码的真实码值
        String code = lineCaptcha.getCode().toLowerCase();
        lineCaptcha.write(response.getOutputStream());
    }


}
